package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.JournalGoods;
import com.example.service.JournalGoodsService;
import com.example.service.JournalGoodsService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


@RestController
@RequestMapping("journalGoodss")
public class JournalGoodsController {

    @Autowired
    private JournalGoodsService journalGoodsService;

    /**
     * 分页查询所有商品操作日志信息
     * @return
     */

    @GetMapping("/getPageJournalGoods/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,JournalGoods journalGoods){

        IPage page=new Page(currentPage,pageSize);
        LambdaQueryWrapper<JournalGoods> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(journalGoods.getGoodsName()),JournalGoods::getGoodsName,journalGoods.getGoodsName());

        IPage page1 = journalGoodsService.page(page,lqw);

        return R.success("分页查询所有journalGoodss成功！",page1);
    }


    /**
     * 添加
     * @param journalGoods
     * @return
     */
    @PostMapping("/addJournalGoods")
    public R add(@RequestBody JournalGoods journalGoods){
        System.out.println("==========");
        System.out.println(journalGoods);
        System.out.println("==========");

        Date date = new Date();   //获取当前的日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String dateNow = simpleDateFormat.format(date);//获取String类型的时间
        journalGoods.setTime(dateNow);

        Boolean update = journalGoodsService.save(journalGoods);
        return R.success("添加journalGoods成功!", null);
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteJournalGoods/{id}")
    public R deleteById(@PathVariable int id){

        Boolean byId = journalGoodsService.removeById(id);
        if (byId){
            return R.success("删除journalGoodss成功!",null);
        }
        return R.error("删除journalGoodss失败!");
    }


}

